//
// Created by liushuai on 2021/2/22.
//

#include "reverseStr.h"
// 分段反转2k个字符串的前k个
string reverseStr(string s, int k) {
<<<<<<< HEAD:cxx/algorithm/src/reverseStr.cc
    int start = 0;
    int end = start+2 * k;
    int size = s.size();
    while (end < size) {
        std::reverse(s.begin() + start, s.begin() + start+k);
        start = end;
        end = start+2*k;
    }
    if(start == 0){
        if(start+k<=size)
            std::reverse(s.begin(),s.begin()+k);
        else
            std::reverse(s.begin(),s.end());
    }else{
        if(start+k<=size)
            std::reverse(s.begin()+start,s.begin()+start+k);
        else
            std::reverse(s.begin()+start,s.end());;
    }

    return s;

=======
  int start = 0;
  int end = start + 2 * k;
  int size = s.size();
  while (end < size) {
    std::reverse(s.begin() + start, s.begin() + start + k);
    start = end;
    end = start + 2 * k;
  }
  if (start == 0) {
    if (start + k <= size)
      std::reverse(s.begin(), s.begin() + k);
    else
      std::reverse(s.begin(), s.end());
  } else {
    if (start + k <= size)
      std::reverse(s.begin() + start, s.begin() + start + k);
    else
      std::reverse(s.begin() + start, s.end());
    ;
  }

  return s;
>>>>>>> dev:cxx/algorithm/common/src/reverseStr.cc
}